//
//  LJJGoodsListTopButtonView.m
//  shopTest
//
//  Created by 梁佳佳 on 2016/11/21.
//  Copyright © 2016年 梁佳佳. All rights reserved.
//

#import "LJJGoodsListTopButtonView.h"

@interface LJJGoodsListTopButtonView ()

@property (strong, nonatomic) UIButton *hostButton;    /** 热门 **/
@property (strong, nonatomic) UIButton *priceButton;    /** 价格 **/
@property (strong, nonatomic) UIButton *goodButton;    /** 好评 **/
@property (strong, nonatomic) UIButton *newsButton;    /** 新品 **/
@property (strong, nonatomic) UIView *lineView;    /** 线 **/
@property (strong, nonatomic) UIButton *tempButton;    /** 中间tempButton **/

@end

@implementation LJJGoodsListTopButtonView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.hostButton];
        [self addSubview:self.priceButton];
        [self addSubview:self.goodButton];
        [self addSubview:self.newsButton];
        [self addSubview:self.lineView];
        [self addLayouot];
        self.tempButton = self.hostButton;
    }
    return self;
}

- (void)addLayouot
{
    __weak typeof(self) weakSelf = self;
    [_hostButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(weakSelf);
        make.width.mas_offset(WIDTH / 4);
    }];
    [_priceButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(weakSelf);
        make.width.mas_offset(WIDTH / 4);
        make.left.equalTo(_hostButton.mas_right);
    }];
    [_goodButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(weakSelf);
        make.width.mas_offset(WIDTH / 4);
        make.left.equalTo(_priceButton.mas_right);
    }];
    [_newsButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(weakSelf);
        make.width.mas_equalTo(WIDTH / 4);
        make.left.equalTo(_goodButton.mas_right);
    }];
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_offset(CGSizeMake(40, 2));
        make.bottom.equalTo(weakSelf);
        make.centerX.equalTo(_hostButton.mas_centerX);
    }];
}
- (void)fourBthTouchMethod:(UIButton *)sender
{
    sender.selected = YES;
    self.tempButton.selected = NO;
    self.tempButton = sender;
    __weak typeof(self) weakSelf = self;
    [_lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(40, 2));
        make.bottom.equalTo(weakSelf);
        make.centerX.equalTo(self.tempButton.mas_centerX);
    }];
    [UIView animateWithDuration:0.3 animations:^{
        [self layoutIfNeeded];//更新布局
    }];
}
#pragma mark -
#pragma mark - lazy method
- (UIButton *)hostButton
{
    if (!_hostButton) {
        _hostButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_hostButton setTitle:@"热门" forState:UIControlStateNormal];
        _hostButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
        [_hostButton setTitleColor:RGB(162, 162, 162, 1) forState:UIControlStateNormal];
        [_hostButton setTitleColor:RGB(53, 161, 241, 1) forState:UIControlStateSelected];
        _hostButton.selected = YES;
        [_hostButton addTarget:self action:@selector(fourBthTouchMethod:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _hostButton;
}
- (UIButton *)priceButton
{
    if (!_priceButton) {
        _priceButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_priceButton setTitle:@"价格" forState:UIControlStateNormal];
        _priceButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
        [_priceButton setTitleColor:RGB(162, 162, 162, 1) forState:UIControlStateNormal];
        [_priceButton setTitleColor:RGB(53, 161, 241, 1) forState:UIControlStateSelected];
        _priceButton.selected = NO;
        [_priceButton addTarget:self action:@selector(fourBthTouchMethod:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _priceButton;
}
- (UIButton *)goodButton
{
    if (!_goodButton) {
        _goodButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_goodButton setTitle:@"好评" forState:UIControlStateNormal];
        _goodButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
        [_goodButton setTitleColor:RGB(162, 162, 162, 1) forState:UIControlStateNormal];
        [_goodButton setTitleColor:RGB(53, 161, 241, 1) forState:UIControlStateSelected];
        _goodButton.selected = NO;
        [_goodButton addTarget:self action:@selector(fourBthTouchMethod:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _goodButton;
}
- (UIButton *)newsButton
{
    if (!_newsButton) {
        _newsButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_newsButton setTitle:@"新品" forState:UIControlStateNormal];
        _newsButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
        [_newsButton setTitleColor:RGB(162, 162, 162, 1) forState:UIControlStateNormal];
        [_newsButton setTitleColor:RGB(53, 161, 241, 1) forState:UIControlStateSelected];
        _newsButton.selected = NO;
        [_newsButton addTarget:self action:@selector(fourBthTouchMethod:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _newsButton;
}
- (UIView *)lineView
{
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = RGB(56, 166, 243, 1);
    }
    return _lineView;
}

@end
